<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '':'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem :label="t('sales.debtortrans.transno')" type="number" prop="transno" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.transno" :placeholder="t('Please input field', { field: t('sales.debtortrans.transno') })" />
                    <FormItem :label="t('sales.debtortrans.type')" type="remoteSelects" v-model="baTable.form.items!.type" prop="type" :input-attr="{ pk: 'cncerp_setup_systypes.typeid', field: 'typename', 'remote-url': '/admin/setup.Systypes/index' }" :placeholder="t('Please select field', { field: t('sales.debtortrans.type') })" />
                    <FormItem :label="t('sales.debtortrans.debtorno')" type="remoteSelect" v-model="baTable.form.items!.debtorno" prop="debtorno" :input-attr="{ pk: 'cncerp_sales_debtorsmaster.debtorno', field: 'name', 'remote-url': '/admin/sales.Debtorsmaster/index' }" :placeholder="t('Please select field', { field: t('sales.debtortrans.debtorno') })" />
                    <FormItem :label="t('sales.debtortrans.branchcode')" type="remoteSelect" v-model="baTable.form.items!.branchcode" prop="branchcode" :input-attr="{ pk: 'cncerp_sales_custbranch.branchcode', field: 'brname', 'remote-url': '/admin/sales.Custbranch/index' }" :placeholder="t('Please select field', { field: t('sales.debtortrans.branchcode') })" />
                    <FormItem :label="t('sales.debtortrans.trandate')" type="date" v-model="baTable.form.items!.trandate" prop="trandate" :placeholder="t('Please select field', { field: t('sales.debtortrans.trandate') })" />
                    <FormItem :label="t('sales.debtortrans.inputdate')" type="datetime" v-model="baTable.form.items!.inputdate" prop="inputdate" :placeholder="t('Please select field', { field: t('sales.debtortrans.inputdate') })" />
                    <FormItem :label="t('sales.debtortrans.prd')" type="remoteSelect" v-model="baTable.form.items!.prd" prop="prd" :input-attr="{ pk: 'cncerp_fm_periods.periodno', field: 'lastdate_in_period', 'remote-url': '/admin/fm.Periods/index' }" :placeholder="t('Please select field', { field: t('sales.debtortrans.prd') })" />
                    <FormItem :label="t('sales.debtortrans.settled')" type="radio" v-model="baTable.form.items!.settled" prop="settled" :data="{ content: { '0': t('sales.debtortrans.settled 0'), '1': t('sales.debtortrans.settled 1') } }" :placeholder="t('Please select field', { field: t('sales.debtortrans.settled') })" />
                    <FormItem :label="t('sales.debtortrans.reference')" type="string" v-model="baTable.form.items!.reference" prop="reference" :placeholder="t('Please input field', { field: t('sales.debtortrans.reference') })" />
                    <FormItem :label="t('sales.debtortrans.tpe')" type="remoteSelect" v-model="baTable.form.items!.tpe" prop="tpe" :input-attr="{ pk: 'cncerp_sales_salestypes.typeabbrev', field: 'sales_type', 'remote-url': '/admin/sales.Salestypes/index' }" :placeholder="t('Please select field', { field: t('sales.debtortrans.tpe') })" />
                    <FormItem :label="t('sales.debtortrans.order_')" type="number" prop="order_" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.order_" :placeholder="t('Please input field', { field: t('sales.debtortrans.order_') })" />
                    <FormItem :label="t('sales.debtortrans.rate')" type="number" prop="rate" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.rate" :placeholder="t('Please input field', { field: t('sales.debtortrans.rate') })" />
                    <FormItem :label="t('sales.debtortrans.ovamount')" type="number" prop="ovamount" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.ovamount" :placeholder="t('Please input field', { field: t('sales.debtortrans.ovamount') })" />
                    <FormItem :label="t('sales.debtortrans.ovgst')" type="number" prop="ovgst" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.ovgst" :placeholder="t('Please input field', { field: t('sales.debtortrans.ovgst') })" />
                    <FormItem :label="t('sales.debtortrans.ovfreight')" type="number" prop="ovfreight" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.ovfreight" :placeholder="t('Please input field', { field: t('sales.debtortrans.ovfreight') })" />
                    <FormItem :label="t('sales.debtortrans.ovdiscount')" type="number" prop="ovdiscount" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.ovdiscount" :placeholder="t('Please input field', { field: t('sales.debtortrans.ovdiscount') })" />
                    <FormItem :label="t('sales.debtortrans.diffonexch')" type="number" prop="diffonexch" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.diffonexch" :placeholder="t('Please input field', { field: t('sales.debtortrans.diffonexch') })" />
                    <FormItem :label="t('sales.debtortrans.alloc')" type="number" prop="alloc" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.alloc" :placeholder="t('Please input field', { field: t('sales.debtortrans.alloc') })" />
                    <FormItem :label="t('sales.debtortrans.invtext')" type="textarea" v-model="baTable.form.items!.invtext" prop="invtext" :input-attr="{ rows: 3 }" @keyup.enter.stop="" @keyup.ctrl.enter="baTable.onSubmit(formRef)" :placeholder="t('Please input field', { field: t('sales.debtortrans.invtext') })" />
                    <FormItem :label="t('sales.debtortrans.shipvia')" type="number" prop="shipvia" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.shipvia" :placeholder="t('Please input field', { field: t('sales.debtortrans.shipvia') })" />
                    <FormItem :label="t('sales.debtortrans.edisent')" type="number" prop="edisent" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.edisent" :placeholder="t('Please input field', { field: t('sales.debtortrans.edisent') })" />
                    <FormItem :label="t('sales.debtortrans.consignment')" type="string" v-model="baTable.form.items!.consignment" prop="consignment" :placeholder="t('Please input field', { field: t('sales.debtortrans.consignment') })" />
                    <FormItem :label="t('sales.debtortrans.packages')" type="number" prop="packages" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.packages" :placeholder="t('Please input field', { field: t('sales.debtortrans.packages') })" />
                    <FormItem :label="t('sales.debtortrans.salesperson')" type="remoteSelect" v-model="baTable.form.items!.salesperson" prop="salesperson" :input-attr="{ pk: 'cncerp_sales_salesman.salesmancode', field: 'salesmanname', 'remote-url': '/admin/sales.Salesman/index' }" :placeholder="t('Please select field', { field: t('sales.debtortrans.salesperson') })" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance, FormItemRule } from 'element-plus'
import { inject, reactive, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    transno: [buildValidatorData({ name: 'number', title: t('sales.debtortrans.transno') }), buildValidatorData({ name: 'required', title: t('sales.debtortrans.transno') })],
    type: [buildValidatorData({ name: 'required', title: t('sales.debtortrans.type') }), buildValidatorData({ name: 'number', title: t('sales.debtortrans.type') })],
    debtorno: [buildValidatorData({ name: 'required', title: t('sales.debtortrans.debtorno') })],
    branchcode: [buildValidatorData({ name: 'required', title: t('sales.debtortrans.branchcode') })],
    trandate: [buildValidatorData({ name: 'date', title: t('sales.debtortrans.trandate') })],
    inputdate: [buildValidatorData({ name: 'date', title: t('sales.debtortrans.inputdate') }), buildValidatorData({ name: 'required', title: t('sales.debtortrans.inputdate') })],
    prd: [buildValidatorData({ name: 'required', title: t('sales.debtortrans.prd') })],
    settled: [buildValidatorData({ name: 'required', title: t('sales.debtortrans.settled') })],
    order_: [buildValidatorData({ name: 'number', title: t('sales.debtortrans.order_') }), buildValidatorData({ name: 'required', title: t('sales.debtortrans.order_') })],
    rate: [buildValidatorData({ name: 'number', title: t('sales.debtortrans.rate') })],
    ovamount: [buildValidatorData({ name: 'number', title: t('sales.debtortrans.ovamount') })],
    ovgst: [buildValidatorData({ name: 'number', title: t('sales.debtortrans.ovgst') })],
    ovfreight: [buildValidatorData({ name: 'number', title: t('sales.debtortrans.ovfreight') })],
    ovdiscount: [buildValidatorData({ name: 'number', title: t('sales.debtortrans.ovdiscount') })],
    diffonexch: [buildValidatorData({ name: 'number', title: t('sales.debtortrans.diffonexch') })],
    alloc: [buildValidatorData({ name: 'number', title: t('sales.debtortrans.alloc') })],
    shipvia: [buildValidatorData({ name: 'number', title: t('sales.debtortrans.shipvia') })],
    edisent: [buildValidatorData({ name: 'number', title: t('sales.debtortrans.edisent') })],
    packages: [buildValidatorData({ name: 'number', title: t('sales.debtortrans.packages') })],
})
</script>

<style scoped lang="scss"></style>
